<?php

namespace fastflow\fa;

use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\fastflow\flow\Flow;
use think\Session;
use fastflow\api;


class FlowAuth
{

    public function _initialize()
    {

    }


    public function checkStartAuth($flow_id)
    {
        $flowAuth_row = (new \app\admin\model\fastflow\flow\FlowAuth())->where(['flow_id' => $flow_id])->find();
        if (!$flowAuth_row) {
            return false;
        }
        if ($flowAuth_row['rules'] == '') {
            return false;
        }
        $rules = json_decode($flowAuth_row['rules'], true);
        if (!isset($rules['start'])) {
            return false;
        }
        $startRules = $rules['start'];
        if ($startRules == ''){
            return false;
        }
        $admin_id = Session::get()['admin']['id'];
        $groupAccess = AuthGroupAccess::where('uid', $admin_id)->select();

        foreach ($startRules as $startRule) {
            if ($startRule['behavior'] == 1) {
                if ($startRule['worker'] == ''){
                    return true;
                }
                $startRule['worker'] = explode(',', $startRule['worker']);
                if ($startRule['scope'] == 1) {
                    if (in_array($admin_id, $startRule['worker'])) {
                        return true;
                    }
                }
                elseif ($startRule['scope'] == 2) {
                    foreach ($groupAccess as $access) {
                        $groupId = $access['group_id'];
                        if (in_array($groupId, $startRule['worker'])) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    //允许编辑的字段 如果为true允许编辑全表字段
    public function getCanEditFields($bill, $bill_id, $thread_id = null)
    {
        $api = new api();
        $can_edit_fields = [];

        $isSuperManager = false;
        $admin_id = Session::get()['admin']['id'];
        $groupAccess = AuthGroupAccess::where('uid', $admin_id)->select();
        foreach ($groupAccess as $access) {
            $groupId = $access['group_id'];
            $group = AuthGroup::find($groupId);
            if ($group['pid'] == 0) {
                $isSuperManager = true;
            }
        }
        if ($isSuperManager) {
            return true;
        }

        $bill_row = $api->getBillRow($bill, $bill_id);
        if (!$bill_row) {
            return [];
        }
        $bill_auth_info = $api->getBillAuthInfo($bill_row['initiator_id'], $bill, $bill_id);

        $code = $bill_auth_info['code'];

        if ($code == 0) {
            return true;
        }
        elseif ($code == 1) {
            $thread = $api->getThreadById($thread_id);
            if (!$thread) {
                return [];
            }
            else {
                if ($thread['status'] != 1) {
                    return [];
                }
                else {
                    if ($api->checkThreadAuth($thread) == false) {
                        return [];
                    }
                }
            }
            $bill_process = $api->getProcessByBill($bill, $bill_id);
            $flow_id = $bill_process['flow_id'];
            $flowAuth_row = (new \app\admin\model\fastflow\flow\FlowAuth())->where(['flow_id' => $flow_id])->find();
            if (!$flowAuth_row) {
                return [];
            }
            if ($flowAuth_row['rules'] == '') {
                return false;
            }
            $rules = json_decode($flowAuth_row['rules'], true);
            if (!isset($rules['edit'])) {
                return false;
            }
            $editRules = $rules['edit'];
            if ($editRules == '') {
                return [];
            }

            foreach ($editRules as $editRule) {
                if ($editRule['steps'] == '') {
                    if ($editRule['fields'] == '') {
                        return true;
                    }
                    else {
                        $can_edit_fields = array_merge($can_edit_fields, explode(',', $editRule['fields']));
                    }
                }
                else {
                    if (in_array($thread['step_id'], explode(',', $editRule['steps']))) {
                        if ($editRule['fields'] == '') {
                            return true;
                        }
                        else {
                            $can_edit_fields = array_merge($can_edit_fields, explode(',', $editRule['fields']));
                        }
                    }
                }

            }
            return array_unique($can_edit_fields);
        }

        return [];

    }


    public function getBillFields($bill)
    {
        $api = new api();
        $bill_fileds = $api->getFieldsWithComment($bill);
        $fields = [];
        foreach ($bill_fileds as $bill_filed) {
            $fields[] = $bill_filed['field'];
        }
        return $fields;
    }

}